﻿namespace Innovative.ProXr
{
	public static partial class DirectApi
	{
		/// <summary>
		/// Multiple NCD Devices can be connected to a single serial port and controlled individually. This example 
		/// shows an R16 and an R32 connected to a single serial port. Before using this wiring configuration, each 
		/// device must be programmed with a unique device number (See E3C Commands in Both Manuals for Details). Once 
		/// a device number has been stored into each controller this wiring configuration may be used to control up 
		/// to 256 different relay boards or other NCD devices in any combination. This wiring configuration only 
		/// allows 2-way communication with the R32. Relay status information cannot be read from the R16. When all 
		/// boards are first powered up, all devices will respond to incoming commands. Use E3C Command 252 to speak 
		/// to one device at a time. Send 252, 0, any subsequent commands should be for the R32, Device 0. Send 252, 1, 
		/// any subsequent commands should be for the R16, Device 1. This E3C Command 252 is useful when mixing different 
		/// types of controllers on a single serial port.
		/// </summary>
		public static class E3cDeviceControl
		{
			/// <summary>
			/// This command operates in configuration mode only (all dip switches in
			/// the off position). This command restores many internal parameters to
			/// the factory default settings. See below for a detailed list:<br/>
			/// E3C Device Number is Set to 0<br/>
			/// Auto Refresh is Activated<br/>
			/// Default Timing is Set to 26,576<br/>
			/// Relay Command Repetitions is Set to 1<br/>
			/// Character Delay is Set to 35<br/>
			/// Reporting Mode is Turned On<br/>
			/// Attached Relay Banks is Set to 32<br/>
			/// </summary>
			public static RelayCommandSet<bool> FactoryReset = new RelayCommandSet<bool>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 50),
					RelayCommand.CreateReadOnlyCommand(2, 144)
				},
				ExpectedResults = new RelayByte[] { 85 },
				ResponseConverter = new Boolean85ResponseConverter()
			};

			/// <summary>
			/// Tells all devices to respond to your commands.
			/// </summary>
			public static RelayCommandSet<Response> EnableAllDevices = new RelayCommandSet<Response>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 248)
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter()
			};

			/// <summary>
			/// Tells all devices to ignore your commands.
			/// </summary>
			public static RelayCommandSet<Response> DisableAllDevices = new RelayCommandSet<Response>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 249)
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter()
			};

			/// <summary>
			/// Tells a specific device to listen to your commands.
			/// </summary>
			public static RelayCommandSet<Response, int> EnableSelectedDevice = new RelayCommandSet<Response, int>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 250),
					RelayCommand.CreateNonOptionalParameterCommand(2, (new RelayByte[0]).CreateRange(0, 255), "Device", "ID of device to control."),
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter(),
				Parameter1Converter = new Int32ParameterConverter()
			};

			/// <summary>
			/// Tells a specific device to ignore your commands.
			/// </summary>
			public static RelayCommandSet<Response, int> DisableSelectedDevice = new RelayCommandSet<Response, int>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 251),
					RelayCommand.CreateNonOptionalParameterCommand(2, (new RelayByte[0]).CreateRange(0, 255), "Device", "ID of device to control."),
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter(),
				Parameter1Converter = new Int32ParameterConverter()
			};

			/// <summary>
			/// Tells a specific device to listen to your commands, all other devices will ignore your commands.
			/// </summary>
			public static RelayCommandSet<Response, int> EnableSelectedDeviceOnly = new RelayCommandSet<Response, int>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 252),
					RelayCommand.CreateNonOptionalParameterCommand(2, (new RelayByte[0]).CreateRange(0, 255), "Device", "ID of device to control."),
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter(),
				Parameter1Converter = new Int32ParameterConverter()
			};

			/// <summary>
			/// Tells a specific device to ignore your commands, all others will listen.
			/// </summary>
			public static RelayCommandSet<Response, int> DisableSelectedDeviceOnly = new RelayCommandSet<Response, int>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 253),
					RelayCommand.CreateNonOptionalParameterCommand(2, (new RelayByte[0]).CreateRange(0, 255), "Device", "ID of device to control."),
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter(),
				Parameter1Converter = new Int32ParameterConverter()
			};

			/// <summary>
			/// This command should only be used when a single device is attached to your serial port. Extended commands 
			/// will report back to the computer.<br/>
			/// Stores the device number into the controller. The device number takes effect immediately. The enabled/disabled 
			/// status of the device is unchanged.<br/>
			/// ProXR Series Controllers Only Allow you to use the “Store E3C Device Number” Function while in “Configuration Mode”. 
			/// This prevents accidental modification of the E3C device number under normal operation if an incorrect series of 
			/// commands are sent to the controller. So make sure the ProXR Controller is in configuration mode when storing the E3C device
			/// number. Simply turn off all DIP Switches and Power Cycle the controller to enter configuration mode.
			/// </summary>
			public static RelayCommandSet<Response, int> StoreDeviceNumber = new RelayCommandSet<Response, int>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 255),
					RelayCommand.CreateNonOptionalParameterCommand(2, (new RelayByte[0]).CreateRange(0, 255), "Device", "ID of device to control."),
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter(),
				Parameter1Converter = new Int32ParameterConverter()
			};

			/// <summary>
			/// This command should only be used when a single device is attached to your serial port. Extended commands 
			/// will report back to the computer.<br/>
			/// This command reports back 4 bytes of data:<br/>
			/// ProXR Device ID Part 1: 1<br/>
			/// ProXR Device ID Part 2: 0<br/>
			/// ProXR Firmware Version: 17 (or newer)<br/>
			/// ProXR Year of Firmware Production: 205 (or newer)<br/>
			/// ProXR E3C Device Number
			/// </summary>
			public static RelayCommandSet<Response> RecallDeviceIdentification = new RelayCommandSet<Response>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 246)
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter()
			};

			/// <summary>
			/// This command should only be used when a single device is attached to your serial port. Extended commands 
			/// will report back to the computer.<br/>
			/// 
			/// </summary>
			public static RelayCommandSet<Response> RecallDeviceNumber = new RelayCommandSet<Response>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 247)
				},
				ExpectedResults = new RelayByte[] { },
				ResponseConverter = new NoResponseConverter()
			};
		}
	}
}
